package org.ndx.majick.ui.file;

import java.io.File;

import javax.swing.JComponent;
import javax.swing.JLabel;

import org.ndx.majick.properties.Property;
import org.ndx.majick.ui.IClassUIProvider;
import org.ndx.majick.ui.IPropertyUIMetadata;
import org.ndx.majick.ui.IUIProvider;
import org.ndx.majick.ui.object.ObjectUIProvider;

public class FileUIProvider extends ObjectUIProvider implements
		IClassUIProvider {
	public static final String EDIT_BUTTON = ".editButton";
	public static final String PATH_FIELD = ".pathField";

	@Override
	public FilePropertyMetadata createMetadata() {
		return new FilePropertyMetadata();
	}
	
	@Override
	public JComponent getEditor(IPropertyUIMetadata metadata, Property<?> source) {
		FilePropertyMetadata fileMeta = (FilePropertyMetadata) metadata;
		Property<File> fileProperty = (Property<File>) source;
		JComponent returned = new FilePathEditor(fileMeta, fileProperty);
		configureEditor(returned, metadata, source);
		return returned;
	}

	/**
	 * Adds some naming for our internal components
	 */
	@Override
	protected void configureEditor(JComponent generated,
			IPropertyUIMetadata metadata, Property<?> source) {
		super.configureEditor(generated, metadata, source);
		FilePathEditor editor = (FilePathEditor) generated;
		editor.getPathField().setName(source.getName()+IUIProvider.EDITOR+PATH_FIELD);
		editor.getEditButton().setName(source.getName()+IUIProvider.EDITOR+EDIT_BUTTON);
	}
	
	/**
	 * Viewer simply allows one to view a file path segment configured
	 */
	@Override
	public JComponent getViewer(IPropertyUIMetadata metadata, Property<?> source) {
		FilePropertyMetadata fileMeta = (FilePropertyMetadata) metadata;
		JLabel viewText = new JLabel();
		AbstractViewUpdater<JLabel> updater = new AbstractViewUpdater<JLabel>(viewText, fileMeta, (Property<File>) source) {

			@Override
			protected void setViewText(String path) {
				viewComponent.setText(path);
			}
			
		};
		configureViewer(viewText, metadata, source);
		return viewText;
	}
}
